package com.sun.module_home.ui

import android.os.Build
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.DarkMode
import androidx.compose.material.icons.filled.FiberManualRecord
import androidx.compose.material.icons.filled.Palette
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.PrimaryTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.Text
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import com.sun.module_base.model.AppThemeState
import com.sun.module_base.theme.ColorPallet
import com.sun.module_base.theme.blue500
import com.sun.module_base.theme.green500
import com.sun.module_base.theme.orange500
import com.sun.module_base.theme.purple
import com.sun.module_home.ui.index.IndexScreen
import com.sun.module_home.ui.questions.QuestionsScreen
import com.sun.module_home.ui.square.SquareScreen
import com.sun.module_network.utils.logi
import kotlinx.coroutines.launch


/**
 *@author:sunc
 *@time :2025/7/29 16:57
 *@desc: 描述
 */


sealed class Destination(
    var title: String,
) {
    object Index : Destination("首页")
    object Square : Destination("广场")
    object Questions : Destination("问答")
}

@OptIn(ExperimentalMaterial3Api::class)
@Preview(showBackground = true)
@Composable
fun HomeScreen(
    modifier: Modifier = Modifier,
    appThemeState: MutableState<AppThemeState> = mutableStateOf(AppThemeState())
) {
    "HomeScreen".logi()
//    var selectIndex by rememberSaveable { mutableIntStateOf(0) }
    val list = listOf(Destination.Index, Destination.Square, Destination.Questions)
    val pagerState = rememberPagerState { list.size }

    val scope = rememberCoroutineScope()
    val showMenu = remember { mutableStateOf(false) }
//    OptimizedStatusBar(themeColor)

    Box(modifier = modifier.fillMaxSize()) {
        Column(modifier = modifier.fillMaxSize()) {
            Row(modifier = Modifier.fillMaxWidth()) {
//                SecondaryTabRow( //用于次级导航标签页（如子页面内的分类切换）
                PrimaryTabRow( // 用于主导航标签页（如应用主功能切换）
                    selectedTabIndex = pagerState.currentPage,
                    modifier = Modifier.weight(1f)
                ) {
                    list.forEachIndexed { index, item ->
                        Tab(
                            selected = pagerState.currentPage == index,
                            text = {
                                Text(
                                    text = item.title,
                                    maxLines = 1,
                                    overflow = TextOverflow.Ellipsis
                                )
                            },
                            onClick = {
                                scope.launch {
                                    pagerState.animateScrollToPage(index)
                                }
                            }
                        )
                    }
                }
                IconButton(onClick = {
                    appThemeState.value =
                        appThemeState.value.copy(darkTheme = !appThemeState.value.darkTheme)
                }) {
                    Icon(
                        imageVector = Icons.Default.DarkMode,
                        contentDescription = null,
                    )
                }
                IconButton(
                    onClick = {
                        showMenu.value = !showMenu.value
                    }
                ) {
                    Icon(Icons.Default.Palette, contentDescription = null)
                }
            }
            HorizontalDivider()
            HorizontalPager(state = pagerState) {
//                key (appThemeState.value){
                    when (it) {
                        0 -> IndexScreen(appThemeState = appThemeState)
                        1 -> SquareScreen()
                        2 -> QuestionsScreen()
                    }
//                }
            }

        }
        if (showMenu.value) {
            PalletMenu(onPalletChange = {
                appThemeState.value = appThemeState.value.copy(pallet = it)
                showMenu.value = false
            }, expanded = showMenu)
        }

    }
}

@Composable
fun PalletMenu(
    onPalletChange: (ColorPallet) -> Unit,
    expanded: MutableState<Boolean>
) {
    val list = mutableListOf(
        Pair(green500, ColorPallet.GREEN),
        Pair(purple, ColorPallet.PURPLE),
        Pair(orange500, ColorPallet.ORANGE),
        Pair(blue500, ColorPallet.BLUE)
    )
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        list.add(Pair(dynamicLightColorScheme(LocalContext.current).primary, ColorPallet.WALLPAPER))
    }

    DropdownMenu(
        expanded = expanded.value,
        offset = DpOffset(200.dp, 78.dp),
        onDismissRequest = {
            expanded.value = false
        }) {
        list.forEach { item ->
            run {
                DropdownMenuItem(
                    text = {
                        Row(
                            modifier = Modifier.padding(8.dp),
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Icon(
                                imageVector = Icons.Filled.FiberManualRecord,
                                tint = item.first,
                                contentDescription = null
                            )
                            Text(text = item.second.name, modifier = Modifier.padding(8.dp))
                        }
                    },
                    onClick = {
                        onPalletChange.invoke(item.second)
                    })
            }
        }

    }


}
